//
//  SpecialDetailViewController.m
//  LovingPartyBuilding
//
//  Created by ZJ on 2017/5/2.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "SpecialDetailViewController.h"
#import "ZJStudySpecialDetailHeaderView.h"
#import "NetWorkingManager.h"
#import "ZJRefurbish.h"
#import "ZJStudySpecialDetailTableViewCell.h"
#define CellID  @"ZJStudySpecialDetailTableViewCell"
@interface SpecialDetailViewController ()<UITableViewDelegate,UITableViewDataSource>

/** tableView */
@property (nonatomic, strong) UITableView *tableView;
/** dataArray */
@property (nonatomic, strong) NSMutableArray *dataArray;
/** headerView */
@property (nonatomic, strong) ZJStudySpecialDetailHeaderView *headerView;
/** 刷新 */
@property (nonatomic, strong) ZJRefurbish *refurbish;
/**  分页  */
@property (assign, nonatomic) NSInteger  page;
@end

@implementation SpecialDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if (self.relid.length != 0) {
        self.titlelabel.text = self.classname;
    }else{
        self.titlelabel.text = self.spcialInfo.CLASS_NAME;
    }
    
    self.view.backgroundColor = [UIColor viewBGGrayColor];
    __weak typeof(self) weakSelf = self;
    [self.refurbish headerToScollview:self.tableView Block:^{
        weakSelf.page = 1;
        [weakSelf netWorking_header];
    }];
    [self.refurbish footerToScollview:self.tableView Block:^{
        weakSelf.page ++;
        [weakSelf netWorking_footer];
    }];
    [self.tableView.mj_header beginRefreshing];
    [self setupUI];
}


- (void)setupUI{

    [self headerView];
    [self.view addSubview:self.tableView];
}

- (void)netWorking_header{
    NSString *classid;
    if (self.relid.length != 0) {
        classid = self.relid;
    }else{
        classid = self.spcialInfo.CLASS_ID;
    }
    
    NSDictionary *dict = @{@"PAGE_NUMBER":[NSString stringWithFormat:@"%ld",_page],@"SHOW_NUMBER":@"10",@"CLASSID":classid};
    __weak typeof(self) weakSelf = self;
    [NetWorkingManager postWithUrlString:[NetWorkingURL listCoursewareByClass] Parameter:dict Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {//请求成功
            
            weakSelf.dataArray = [[NSArray yy_modelArrayWithClass:[ZJStudySpacialDetailModel class] json:object[@"COURSEWARE_IN_CLASS"]] mutableCopy];
            [weakSelf.tableView reloadData];
        }
        [weakSelf.tableView.mj_header endRefreshing];
    } Fail:^(id object) {
        [weakSelf.tableView.mj_header endRefreshing];
    }];
}
- (void)netWorking_footer{
    NSString *classid;
    if (self.relid.length != 0) {
        classid = self.relid;
    }else{
        classid = self.spcialInfo.CLASS_ID;
    }
    
    NSDictionary *dict = @{@"PAGE_NUMBER":[NSString stringWithFormat:@"%ld",_page],@"SHOW_NUMBER":@"10",@"CLASSID":classid};
    __weak typeof(self) weakSelf = self;
    [NetWorkingManager postWithUrlString:[NetWorkingURL listCoursewareByClass] Parameter:dict Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {//请求成功
            
            NSArray *array = [NSArray yy_modelArrayWithClass:[ZJStudySpacialDetailModel class] json:object[@"COURSEWARE_IN_CLASS"]];
            
            [weakSelf.dataArray addObjectsFromArray:array];
            [weakSelf.tableView reloadData];
        }
        [weakSelf.tableView.mj_footer endRefreshing];
    } Fail:^(id object) {
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
}

#pragma mark - UITableView 协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZJStudySpacialDetailModel *model = self.dataArray[indexPath.row];
    ZJStudySpecialDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID forIndexPath:indexPath];
    cell.model = model;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 120 * [UIScreen zj_screenHScale];
}
#pragma mark - Setter


#pragma mark - Getters
- (UITableView *)tableView{
    if (_tableView == nil){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, LP_TopHeight, self.view.width, self.view.height - LP_TopHeight) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableHeaderView = self.headerView;
        [_tableView  registerClass:[ZJStudySpecialDetailTableViewCell class] forCellReuseIdentifier: CellID];
    }
    return _tableView;
}

- (NSMutableArray *)dataArray{
    if (_dataArray == nil){
        _dataArray = [NSMutableArray array];
        
    }
    return _dataArray;
}
- (ZJStudySpecialDetailHeaderView *)headerView{
    if (_headerView == nil){
        _headerView = [[ZJStudySpecialDetailHeaderView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, 10)];
        _headerView.title = self.spcialInfo.CLASS_COMMENT;
    }
    return _headerView;
}

- (ZJRefurbish *)refurbish{
    if (_refurbish == nil){
        _refurbish = [ZJRefurbish new];
        
    }
    return _refurbish;
}

@end
